package com.hzspace.wxtemplatemessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanUserWeixinOpenidGetResult;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Version 1.0
 * @Author hz
 * @Date 2020/12/2 10:31
 */
@Service
public class WXService {

    private static final String appId = "wx9933700696a13d8a";
    private static final String appSecret = "3a3242d58a856e78187c65efe8a0bdd2";
    @Resource
    private YouzanService youzanService;

    public String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        url = url.replace("APPID", appId).replace("APPSECRET", appSecret);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }

    public void sendWxTemplateMessage() throws SDKException {
        String templateId = "kP_91hgSZ-z_7eHxIdrz_tKgSSP0qcJk4pqssmJT8q8";
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", getAccessToken());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject params = new JSONObject();
        YouzanUserWeixinOpenidGetResult wxopenid = youzanService.getWxOpenIdByMobileAndYzOpenId("", "", "");
        System.out.println(wxopenid);
        params.put("touser", wxopenid.getData().getOpenId());
        params.put("template_id", templateId);

        JSONObject miniprogram = new JSONObject();
        miniprogram.put("appid", "wx2c9036bbb0046e67");
        miniprogram.put("pagepath", "index?foo=bar");
        params.put("miniprogram", miniprogram);

        JSONObject data = new JSONObject();
        JSONObject first = new JSONObject();
        first.put("value", "FIRST");
        first.put("color", "#173177");
        data.put("first", first);
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "KEYWORD1");
        keyword1.put("color", "#173177");
        data.put("keyword1", keyword1);
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", "KEYWORD2");
        keyword2.put("color", "#173177");
        data.put("keyword2", keyword2);
        JSONObject remark = new JSONObject();
        remark.put("value", "REMARK");
        remark.put("color", "#173177");
        data.put("remark", remark);
        params.put("data", data);

        HttpEntity httpEntity = new HttpEntity(params, headers);

        RestTemplate restTemplate = new RestTemplate();
        JSONObject result = restTemplate.postForObject(url, httpEntity,JSONObject.class);
        System.out.println(JSON.toJSONString(result));

    }

}
